
#include "AlarmTask.h"
                 
OS_TID alarmdID,alarmAlertID;
xtime alarm_time,tmptime;
int alarmType;

char* AlarmTypeString[4] = {
    " OFF  ",
    " Once ",
    " Daily",
    "Weekly"
};

int sel;

__task void AlarmAlert(void){
    int i,x,y=0,z;
    while(1){         
        print_lcd (0, "### ALARM !! ###");
        print_lcd (3, " HOLD M TO STOP");
        x=Scankey();
        if(x==BACK){y++;}
        if(y>3){break;}
        for (i=0;i<5;i++){
            DrvGPIO_ClrBit(E_GPB, 11);
            DrvGPIO_SetBit(E_GPD, 14);
            //os_dly_wait(80);
            for(x=1;x<80;x++){for(z=1;z<1000;z++){}}
            DrvGPIO_SetBit(E_GPB, 11);
            DrvGPIO_ClrBit(E_GPD, 14);
            for(x=1;x<80;x++){for(z=1;z<1000;z++){}}
        }
        DrvGPIO_ClrBit(E_GPB, 11); 
        DrvGPIO_SetBit(E_GPD, 14); 
        for(x=1;x<400;x++){for(z=1;z<1000;z++){}}
        DrvGPIO_SetBit(E_GPB, 11);
        DrvGPIO_ClrBit(E_GPD, 14);
        for(x=1;x<400;x++){for(z=1;z<1000;z++){}}
    }              
    print_lcd (0, "                "); 
    print_lcd (3, "                ");
    
    os_tsk_delete_self();  
}


__task void alarmd(void){
    int alert = 0,n,temp;
    os_itv_set(1000);
    while(1){  
        print_lcd(2,"(A)");
        os_itv_wait();
        tmptime = RTCGetTime();
        if (alarmType){
            
            if (tmptime.jam==alarm_time.jam&&
                tmptime.menit==alarm_time.menit){
                    if (alarmType==1){
                        alarmType=0;
                        print_lcd(0,"    ");
                        alert = 1;
                    } else if (alarmType>1){
                        if (tmptime.tanggal==alarm_time.tanggal&&
                            tmptime.bulan==alarm_time.bulan&&
                            tmptime.tahun==alarm_time.tahun){
                                alert=1;
                                if (alarmType==2){n = 1;}else{n=7;}
                                while(n){
                                    n--;
                                    tmptime.tanggal==alarm_time.tanggal++;
                                    temp = DayInMonth(alarm_time.bulan, alarm_time.tahun);
                                    if (alarm_time.tanggal > temp) {
                                        alarm_time.tanggal= 1;
                                        ++alarm_time.bulan;
                                        if (alarm_time.bulan > 12) {
                                            alarm_time.bulan = 1;} 
                                    }
                                }
                            }   
                    }
                
                }
            if (alert){
                alert=0;
                alarmAlertID=os_tsk_create(AlarmAlert,   50);
            }
        } else {     
            print_lcd(2,"***");
            os_tsk_delete_self(); 
        }

    }
    
}

__task void TaskBlinkAlarm(void){
    char buffer[20];
    static int temp=0;
    PrintClockString(alarm_time);
    PrintDateString(alarm_time);
    os_itv_set(333);
    while (1){
        os_itv_wait();
        if (temp){
            /*sprintf(buffer," sel = %d ",sel);
            print_lcd(3, buffer);*/ 
            switch (sel) {                   
                case 0:                        
                    sprintf(buffer," ##:%02d [%s] ",alarm_time.menit,AlarmTypeString[alarmType]);
                    break;
                case 1:
                    sprintf(buffer," %02d:## [%s] ",alarm_time.jam,AlarmTypeString[alarmType]);
                    break;
                case 2:
                    sprintf(buffer," %02d:%02d [******] ",alarm_time.jam,alarm_time.menit);
                    break;
                case 3:
                    sprintf(buffer,"*** ## %s 20%d ",getMonName(alarm_time.bulan),alarm_time.tahun);
                    break;           
                case 4:
                    sprintf(buffer,"*** %02d ### 20%02d ",alarm_time.tanggal,alarm_time.tahun);
                    break;
                case 5:
                    sprintf(buffer,"*** %02d %s 20## ",alarm_time.tanggal,getMonName(alarm_time.bulan));
                    break;
                case -1:
                    os_tsk_delete_self();
                    break;
            }
            if (sel<3){
                print_lcd(LCD_CLOCK_POS, buffer);    
            } else {
                print_lcd(LCD_DATE_POS, buffer);
            }
        } else {
            sprintf(buffer," %02d:%02d [%s] *",alarm_time.jam,alarm_time.menit,AlarmTypeString[alarmType]); 
            print_lcd(LCD_CLOCK_POS, buffer); 
            PrintDateString(alarm_time);
        }        
        temp^=1; 
  }             
}

__task void TaskEditAlarm(void){
    
     // isi alarm_time dengan waktu sekarang
    int x,last=5;  
    int temp;
    OS_TID idBlinkAlarmTask;
    if (!alarmType){
        alarm_time = RTCGetTime();
        }
    sel = 0;
    OpenKeyPad();
    //os_tsk_delete(alarmdID);   
    idBlinkAlarmTask = os_tsk_create(TaskBlinkAlarm    ,50);
                             
    while (1){      
        print_lcd(0,"## Set Alarm ###");
        x = Scankey();  
        //penekanan tombol terus menerus tidak dihiraukan
        if (last) {last=x;x=0;}else{last=x;}  
           
        switch (x){
            case UP:
                switch (sel){
                    case 0: ++alarm_time.jam; break;
                    case 1: ++alarm_time.menit; break;
                    case 2: ++alarmType; break;
                    case 3: ++alarm_time.tanggal; break;
                    case 4: ++alarm_time.bulan; break;
                    case 5: ++alarm_time.tahun; break;
                }
                if(sel <3){
                    if (alarm_time.jam>23){alarm_time.jam=0;}
                    if (alarm_time.menit>59){alarm_time.menit=0;}
                    if (alarmType>3){alarmType=0;} 
                } else {
                    if (alarm_time.tahun > 99) {alarm_time.tahun = 0;}
                    if (alarm_time.bulan > 12) {alarm_time.bulan = 1;} 
                    temp = DayInMonth(alarm_time.bulan, alarm_time.tahun);
                    if (alarm_time.tanggal > temp) {alarm_time.tanggal= 1;}
                    if (alarm_time.tanggal < 1) {alarm_time.tanggal = temp;} 
                }                        
                break;
            case DOWN:
                switch (sel){
                    case 0: --alarm_time.jam; break;
                    case 1: --alarm_time.menit; break;
                    case 2: --alarmType; break;
                    case 3: --alarm_time.tanggal; break;
                    case 4: --alarm_time.bulan; break;
                    case 5: --alarm_time.tahun; break;
                }
                if(sel <3){
                    if (alarm_time.jam<0){alarm_time.jam=23;}
                    if (alarm_time.menit<0){ alarm_time.menit=59;}
                    if (alarmType<0){alarmType=3;} 
                }else{
                    if (alarm_time.tahun  < 0) {alarm_time.tahun  = 99;}
                    if (alarm_time.bulan < 1) {alarm_time.bulan = 12;}  
                    temp = DayInMonth(alarm_time.bulan, alarm_time.tahun);
                    if (alarm_time.tanggal > temp) {alarm_time.tanggal= 1;}
                    if (alarm_time.tanggal < 1) {alarm_time.tanggal = temp;}  
                }      
                break;
            case LEFT:
                sel=(sel==0)?5:--sel;
                break;
            case RIGHT:
                sel=(sel==5)?0:++sel;
                break;
            case OK:                                                 
                print_lcd(0,"###  Sukses  ###"); 
                os_tsk_delete(idBlinkAlarmTask);
                sel = -1;
                if (alarmType){
                    alarmdID=os_tsk_create(alarmd,  45);
                } 

                os_dly_wait(700);

                clr_all_pannal(); 
                tmptime=RTCGetTime();
                PrintClockString(tmptime);
                PrintDateString(tmptime);

                                                           
                os_tsk_delete_self();
                break;

            case BACK:      
                print_lcd(0,"## Cancelled ###");
                
                os_tsk_delete(idBlinkAlarmTask);
                os_dly_wait(700); 
                
                clr_all_pannal();
                tmptime=RTCGetTime();
                PrintClockString(tmptime);
                PrintDateString(tmptime);
                                            
                os_tsk_delete_self();
                break;
        } 
    }
}
